home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 December / PC_Format_122008.iso / Multimedia / MediaPortal 0.2.3.0 / MediaPortal_0.2.3.0_Setup.exe / scripts / imdb / mymovies_it.csscript < prev    next >
Text File  |  2007-10-20  |  17KB  |  493 lines

  1. //css_reference "core.dll";
  2. //css_reference "Databases.dll";
  3. //css_reference "utils.dll";
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Net;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Text.RegularExpressions;
  17. using MediaPortal.Util;
  18.  
  19. class Grabber : MediaPortal.Video.Database.IIMDBScriptGrabber
  20. {
  21.   public Grabber()
  22.     {
  23.     }
  24.   void MediaPortal.Video.Database.IIMDBScriptGrabber.FindFilm(string strSearch, int iLimit, ArrayList elements)
  25.   {
  26.     int iCount = 0;
  27.     string strTitle;
  28.     try
  29.     {
  30.       string absoluteUri;
  31.       string strURL = "http://www.mymovies.it/database/ricerca/avanzata/?titolo=" + strSearch + "&titolo_orig=®ista=&attore=&id_genere=-1&nazione=&clausola1=-1&anno_prod=&clausola2=-1&stelle=-1&id_manif=-1&anno_manif=&disponib=-1&ordinamento=1&submit=Inizia+ricerca+%BB";
  32.       string strBody = GetPage(strURL, "utf-8", out absoluteUri);
  33.  
  34.       // Mars Warrior @ 03-sep-2004.
  35.       // First try to find an Exact Match. If no exact match found, just look
  36.       // for any match and add all those to the list. This narrows it down more easily...
  37.       int iStartOfMovieList = strBody.IndexOf("<div class=\"sezione\">Film</div>");
  38.       int endOfTitleList = strBody.IndexOf("<div style=\"height:20px; clear:both;\"></div>");
  39.       if (iStartOfMovieList < 0)
  40.       {
  41.         return;
  42.       }
  43.  
  44.       iStartOfMovieList += "<div class=\"linkblu\" style=\"padding:3px;\">".Length;
  45.       int iEndOfMovieList = strBody.IndexOf("<div style=\"height:20px; clear:both;\"></div>", iStartOfMovieList);
  46.  
  47.       if (iEndOfMovieList < 0)
  48.       {
  49.         iEndOfMovieList = strBody.Length;
  50.       }
  51.       if (endOfTitleList < iEndOfMovieList && endOfTitleList > iStartOfMovieList)
  52.       {
  53.         iEndOfMovieList = endOfTitleList;
  54.       }
  55.       strBody = strBody.Substring(iStartOfMovieList, iEndOfMovieList - iStartOfMovieList);
  56.       while ((true) && (iCount < iLimit))
  57.       {
  58.         ////<A HREF="/Title?0167261">Lord of the Rings: The Two Towers, The (2002)</A>
  59.         int iAHREF = strBody.IndexOf("<a href=");
  60.         if (iAHREF >= 0)
  61.         {
  62.           int iEndAHREF = strBody.IndexOf("</a>");
  63.           if (iEndAHREF >= 0)
  64.           {
  65.             iAHREF += "<a href=.".Length;
  66.             string strAHRef = strBody.Substring(iAHREF, iEndAHREF - iAHREF);
  67.             int iURL = strAHRef.IndexOf("\" title");
  68.             if (iURL > 0)
  69.             {
  70.               strTitle = "";
  71.               strURL = strAHRef.Substring(0, iURL);
  72.               if (strURL[strURL.Length - 1] == '\"')
  73.               strURL = strURL.Substring(0, strURL.Length - 1);
  74.               iURL += "\" title=\"".Length;
  75.               int iURLEnd = strAHRef.IndexOf("\">", iURL);
  76.               if (iURLEnd > 0)
  77.               {
  78.                 strTitle = strAHRef.Substring(iURL, iURLEnd - iURL);
  79.               }
  80.               else
  81.               strTitle = strAHRef.Substring(iURL);
  82.               int iYear = strBody.IndexOf("www.mymovies.it/film/?anno=");
  83.               iYear += "www.mymovies.it/film/.anno=".Length;
  84.               string strYear = strBody.Substring(iYear, 4);
  85.  
  86.               ///int onclick = strURL.IndexOf(" onclick");
  87.               ///if (onclick >= 0)
  88.               ///  strURL = strURL.Substring(0, onclick - 1);
  89.               /// strURL = String.Format("http://it.movies.yahoo.com{0}", strURL);
  90.               HTMLUtil htmlUtil = new HTMLUtil();
  91.               htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  92.  
  93.  
  94.               ///int endTagLength = "</a>".Length;
  95.               ///int posNextTag = strBody.IndexOf("<", iEndAHREF + endTagLength);
  96.               ///if (posNextTag > 0)
  97.               ///{
  98.               ///string strSub = strBody.Substring(iEndAHREF + endTagLength, posNextTag - (iEndAHREF + endTagLength));
  99.               ///strTitle += strSub;
  100.               ///}
  101.               // to avoid including of   
  102.               if ((strTitle.IndexOf("\n") < 0) && (strTitle.IndexOf(" ") < 0))
  103.               {
  104.                 MediaPortal.Video.Database.IMDB.IMDBUrl url = new MediaPortal.Video.Database.IMDB.IMDBUrl(strURL, strTitle + " (" + strYear + ")" + " (mymovies_it)", "mymovies_it");
  105.                 elements.Add(url);
  106.               }
  107.               iCount++;
  108.             }
  109.             if (iEndAHREF + 1 >= strBody.Length)
  110.               break;
  111.             iStartOfMovieList = iEndAHREF + 1;
  112.             iEndAHREF = strBody.IndexOf("<div class=\"tratto\"></div>");
  113.             strBody = strBody.Substring(iEndAHREF + 1);
  114.           }
  115.           else
  116.           {
  117.             break;
  118.           }
  119.         }
  120.         else
  121.         {
  122.           break;
  123.         }
  124.       }
  125.     }
  126.     catch (Exception ex)
  127.     {
  128.       MediaPortal.GUI.Library.Log.Error("exception for mymovies lookup of {0} err:{1} stack:{2}",strSearch, ex.Message, ex.StackTrace);
  129.     }
  130.   }
  131.  
  132.   bool MediaPortal.Video.Database.IIMDBScriptGrabber.GetDetails(MediaPortal.Video.Database.IMDB.IMDBUrl url, ref MediaPortal.Video.Database.IMDBMovie movieDetails)
  133.   {
  134.     try
  135.     {
  136.  
  137.       int iStart = 0;
  138.       int iEnd = 0;
  139.       movieDetails.Reset();
  140.       // add databaseinfo
  141.       // may add an another grabber  
  142.       movieDetails.Database = "mymovies_it";
  143.  
  144.       string strAbsURL;
  145.       string strAbsMovieURL;
  146.       string strUrlMovieURL;
  147.       string strBody = GetPage(url.URL, "utf-8", out strAbsURL);
  148.       strAbsMovieURL = strAbsURL;
  149.       if (strBody == null || strBody.Length == 0)
  150.         return false;
  151.  
  152.       int iPos = strAbsURL.IndexOf("?id=");
  153.       if (iPos > 0)
  154.       {
  155.         iPos += "?id=".Length;
  156.         movieDetails.IMDBNumber = strAbsURL.Substring(iPos);
  157.         int pos = movieDetails.IMDBNumber.IndexOf(".html");
  158.         if (pos > 0)
  159.           movieDetails.IMDBNumber = movieDetails.IMDBNumber.Substring(0, pos);
  160.       }
  161.  
  162.       int imPos = strAbsMovieURL.IndexOf("mymovies.it");
  163.       if (imPos > 0)
  164.       {
  165.         imPos += "mymovies.it".Length;
  166.         strUrlMovieURL = strAbsMovieURL.Substring(imPos);
  167.         int posm = strUrlMovieURL.IndexOf("?id=");
  168.         if (posm > 0)
  169.             strUrlMovieURL = strUrlMovieURL.Substring(0, posm);
  170.       }
  171.       
  172.       url.Title = url.Title.Trim();
  173.       // cut of " (imdb)"
  174.       iEnd = url.Title.IndexOf("[");
  175.       if (iEnd >= 0)
  176.       {
  177.         movieDetails.Title = url.Title.Substring(0, iEnd);
  178.       }
  179.       else
  180.       {
  181.         iEnd = url.Title.IndexOf("(");
  182.         if (iEnd >= 0)
  183.             movieDetails.Title = url.Title.Substring(0, iEnd);
  184.         else
  185.             movieDetails.Title = url.Title;
  186.       }
  187.       movieDetails.Title = movieDetails.Title.Trim();
  188.       string movieTitle = System.Web.HttpUtility.HtmlEncode(movieDetails.Title);
  189.       int iDirectedBy = strBody.IndexOf("Un film di ");
  190.       int iCredits = strBody.IndexOf("Sceneggiatura");
  191.       int iGenre = strBody.IndexOf("Genere ");
  192.       if (iGenre < 0)
  193.          iGenre = strBody.IndexOf("Generi");
  194.       ///int iTagLine = strBody.IndexOf("Tagline:</h5>");
  195.       int iPlotOutline = strBody.IndexOf("<strong class=\"courier\" style=\"font-size:23px; margin-bottom:10px; color:#ff0066; display:block;\">");
  196.       int iPlotSummary = strBody.IndexOf("<span style=\"font-family:Arial, Sans-Serif; font-size:90%; color:#696969\">");
  197.       int iPlot = strBody.IndexOf("<p style=\"text-align:justify;\">");
  198.       string strImgAbsURL = "";
  199.       string strImgBody = GetPage("http://www.mymovies.it/poster/0/?id=" + movieDetails.IMDBNumber, "utf-8", out strImgAbsURL);
  200.       int iImage = strImgBody.IndexOf("alt=\"Locandina");
  201.       if (iImage >= 0)
  202.       {
  203.         iImage = strImgBody.IndexOf("src=\"", iImage);
  204.       }
  205.       int iRating = strBody.IndexOf("title=\"Valutazione ");
  206.       int iCred = strBody.IndexOf("Con ");
  207.       ///int iTop = strBody.IndexOf("Top 250:");
  208.       int iTop = -1;
  209.       int iYear = strBody.IndexOf("http://www.mymovies.it/film/?anno=");
  210.       if (iYear >= 0)
  211.       {
  212.         iYear += "http://www.mymovies.it/film/?anno=".Length;
  213.         string strYear = strBody.Substring(iYear, 4);
  214.         movieDetails.Year = System.Int32.Parse(strYear);
  215.       }
  216.  
  217.       if (iDirectedBy >= 0)
  218.         movieDetails.Director = ParseAHREFIMDB(strBody, iDirectedBy, url.URL).Trim();
  219.  
  220.       if (iCredits >= 0)
  221.         movieDetails.WritingCredits = ParseAHREFIMDB(strBody, iCredits, url.URL).Trim();
  222.  
  223.       if (iGenre >= 0)
  224.       {
  225.         iGenre += "Genere ".Length;
  226.         movieDetails.Genre = ParseGenresIMDB(strBody, iGenre, url.URL).Trim();
  227.       }
  228.       if (iRating >= 0) // and votes
  229.       {
  230.         iRating += "title=\"Valutazione ".Length;
  231.         iStart = iRating;
  232.         if (iStart >= 0)
  233.         {
  234.           ///iStart += "alt=\"".Length;
  235.           iEnd = strBody.IndexOf(" stelle", iStart);
  236.           // set rating
  237.           string strRating = strBody.Substring(iStart, iEnd - iStart);
  238.           if (strRating != String.Empty)
  239.             strRating = strRating.Replace('.', ',');
  240.           try
  241.           {
  242.             movieDetails.Rating = (float)System.Double.Parse(strRating);
  243.             movieDetails.Rating = movieDetails.Rating * 2;
  244.             if (movieDetails.Rating > 10.0f)
  245.               movieDetails.Rating /= 10.0f;
  246.           }
  247.           catch (Exception)
  248.           {
  249.           }
  250.           movieDetails.Votes = "0";
  251.           ///if (movieDetails.Rating != 0.0f)
  252.           ///{
  253.             // now, votes
  254.           ///  movieDetails.Votes = "0";
  255.           ///  iStart = strBody.IndexOf("(", iEnd + 2);
  256.           ///  if (iStart > 0)
  257.           ///  {
  258.           ///    iEnd = strBody.IndexOf(" votes</a>)", iStart);
  259.           ///    if (iEnd > 0)
  260.           ///    {
  261.           ///      iStart += "(<a href=\"ratings\">".Length; // skip the parantese and link before votes
  262.           ///      movieDetails.Votes = strBody.Substring(iStart, iEnd - iStart).Trim();
  263.           ///    }
  264.           ///  }
  265.           ///}
  266.         }
  267.       }
  268.  
  269.       ///if (iTop >= 0) // top rated movie :)
  270.       ///{
  271.       ///  iTop += "top 250:".Length + 2; // jump space and #
  272.       ///  iEnd = strBody.IndexOf("</a>", iTop);
  273.       ///  string strTop = strBody.Substring(iTop, iEnd - iTop);
  274.       ///  movieDetails.Top250 = System.Int32.Parse(strTop);
  275.       ///}
  276.       ///if (iTagLine >= 0)
  277.       ///{
  278.       ///  iTagLine += "Tagline:</h5>".Length;
  279.       ///  iEnd = strBody.IndexOf("<", iTagLine);
  280.       ///  movieDetails.TagLine = strBody.Substring(iTagLine, iEnd - iTagLine).Trim();
  281.       ///  movieDetails.TagLine = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.TagLine);
  282.       ///  movieDetails.TagLine = HttpUtility.HtmlDecode(movieDetails.TagLine);  // Remove HTML entities like ½
  283.       ///}
  284.  
  285.       if (iPlotSummary >= 0)
  286.       {
  287.           iPlotSummary += "<span style=\"font-family:Arial, Sans-Serif; font-size:90%; color:#696969\">".Length;
  288.           iEnd = strBody.IndexOf("</span>", iPlotSummary);
  289.           movieDetails.PlotOutline = strBody.Substring(iPlotSummary, iEnd - iPlotSummary).Trim();
  290.           movieDetails.PlotOutline = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.PlotOutline);
  291.           movieDetails.PlotOutline = HttpUtility.HtmlDecode(movieDetails.PlotOutline);  // remove HTML entities
  292.       }
  293.       if (iPlotOutline >= 0)
  294.       {
  295.         iPlotOutline += "<strong class=\"courier\" style=\"font-size:23px; margin-bottom:10px; color:#ff0066; display:block;\">".Length;
  296.         iPlotOutline ++;
  297.         iEnd = strBody.IndexOf("</strong>", iPlotOutline);
  298.         movieDetails.TagLine = strBody.Substring(iPlotOutline, iEnd - iPlotOutline).Trim();
  299.         movieDetails.TagLine = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.TagLine);
  300.         movieDetails.TagLine = HttpUtility.HtmlDecode(movieDetails.TagLine);
  301.         while (movieDetails.TagLine.IndexOf(" ") == 0)
  302.         {
  303.             movieDetails.TagLine = movieDetails.TagLine.Substring(1);
  304.         }
  305.       }
  306.     
  307.       if (iPlot >= 0) 
  308.       {
  309.         iPlot += "<p style=\"text-align:justify;\">".Length;
  310.         iEnd = strBody.IndexOf("</p>", iPlot);
  311.         movieDetails.Plot = strBody.Substring(iPlot, iEnd - iPlot).Trim();
  312.         movieDetails.Plot = MediaPortal.Util.Utils.stripHTMLtags(movieDetails.Plot);
  313.         movieDetails.Plot = HttpUtility.HtmlDecode(movieDetails.Plot);  // remove HTML entities
  314.       }
  315.  
  316.       if (iImage >= 0)
  317.       {
  318.         iImage += "src=\"".Length;
  319.         iEnd = strImgBody.IndexOf("\"", iImage);
  320.         movieDetails.ThumbURL = strImgBody.Substring(iImage, iEnd - iImage).Trim();
  321.       }
  322.  
  323.       //plot (removed)
  324.      
  325.       int iRunTime = strBody.IndexOf(" minuti");
  326.       iRunTime = iRunTime - 3;
  327.       if (iRunTime > 0)
  328.       {
  329.         string runtime = "";
  330.         runtime = strBody.Substring(iRunTime,3);
  331.         try
  332.         {
  333.            movieDetails.RunTime = Int32.Parse(runtime);
  334.         }
  335.         catch (Exception) { }
  336.       }
  337.  
  338.       int mpaa = strBody.IndexOf("<b>Dizionari</b> (");
  339.       if (mpaa > 0)
  340.       {
  341.         mpaa += "<b>Dizionari</b> ".Length;
  342.         mpaa++;
  343.         movieDetails.MPARating = strBody.Substring(mpaa, 3);
  344.       }
  345.  
  346.       int iCredEnd = strBody.IndexOf("Genere", iCred);
  347.       iCredEnd --;
  348.       iCred += "Con ".Length;
  349.       if (iCred >= 0)
  350.       {
  351.         string strCast = strBody.Substring(iCred, iCredEnd - iCred);
  352.         //movieDetails.Cast += strCast;
  353.         strCast = MediaPortal.Util.Utils.stripHTMLtags(strCast).Trim();
  354.         strCast = HttpUtility.HtmlDecode(strCast);
  355.         strCast = strCast.Substring(0, strCast.Length - 1);
  356.         movieDetails.Cast += strCast;
  357.             ///movieDetails.Cast += strActor;
  358.         movieDetails.Cast += "\n";
  359.       }
  360.  
  361.       return true;
  362.     }
  363.     catch (Exception ex)
  364.     {
  365.       MediaPortal.GUI.Library.Log.Error("exception for mymovies lookup of {0} err:{1} stack:{2}", url.URL, ex.Message, ex.StackTrace);
  366.     }
  367.     return false;
  368.   }
  369.  
  370.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetName()
  371.   {
  372.     return "MYMOVIESIT grabber ";
  373.   }
  374.  
  375.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetLanguage()
  376.   {
  377.     return "IT";
  378.   }
  379.  
  380.   private string GetPage(string strURL, string strEncode, out string absoluteUri)
  381.   {
  382.     string strBody = "";
  383.     absoluteUri = String.Empty;
  384.     Stream ReceiveStream = null;
  385.     StreamReader sr = null;
  386.     WebResponse result = null;
  387.     try
  388.     {
  389.       // Make the Webrequest
  390.       //Log.Info("IMDB: get page:{0}", strURL);
  391.       WebRequest req = WebRequest.Create(strURL);
  392.  
  393.       result = req.GetResponse();
  394.       ReceiveStream = result.GetResponseStream();
  395.  
  396.       // Encoding: depends on selected page
  397.       Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
  398.       sr = new StreamReader(ReceiveStream, encode);
  399.       strBody = sr.ReadToEnd();
  400.  
  401.       absoluteUri = result.ResponseUri.AbsoluteUri;
  402.     }
  403.     catch (Exception)
  404.     {
  405.       //Log.Error("Error retreiving WebPage: {0} Encoding:{1} err:{2} stack:{3}", strURL, strEncode, ex.Message, ex.StackTrace);
  406.     }
  407.     finally
  408.     {
  409.       if (sr != null)
  410.       {
  411.         try
  412.         {
  413.           sr.Close();
  414.         }
  415.         catch (Exception)
  416.         {
  417.         }
  418.       }
  419.       if (ReceiveStream != null)
  420.       {
  421.         try
  422.         {
  423.           ReceiveStream.Close();
  424.         }
  425.         catch (Exception)
  426.         {
  427.         }
  428.       }
  429.       if (result != null)
  430.       {
  431.         try
  432.         {
  433.           result.Close();
  434.         }
  435.         catch (Exception)
  436.         {
  437.         }
  438.       }
  439.     }
  440.     return strBody;
  441.   } // END GetPage()
  442.   string ParseAHREFIMDB(string strBody, int iahref, string strURL)
  443.   {
  444.     int iStart = strBody.IndexOf("<a href=\"", iahref);
  445.     if (iStart < 0)
  446.       iStart = strBody.IndexOf("<A HREF=\"", iahref);
  447.     if (iStart < 0)
  448.       return "";
  449.  
  450.     int iEnd = strBody.IndexOf("</a>", iStart);
  451.     if (iEnd < 0)
  452.       iEnd = strBody.IndexOf("</A>", iStart);
  453.     if (iEnd < 0)
  454.       return "";
  455.  
  456.     iStart += "<a href=\"".Length;
  457.     int iSep = strBody.IndexOf(">", iStart);
  458.     string strurl = strBody.Substring(iStart, (iSep - iStart) - 1);
  459.     iSep++;
  460.     string strTitle = strBody.Substring(iSep, iEnd - iSep);
  461.     strTitle = MediaPortal.Util.Utils.stripHTMLtags(strTitle);
  462.     HTMLUtil htmlUtil = new HTMLUtil();
  463.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  464.     strTitle = strTitle.Trim();
  465.     return strTitle.Trim();
  466.  
  467.   }
  468.   string ParseGenresIMDB(string strBody, int iGenre, string url)
  469.   {
  470.     int iStart = strBody.IndexOf("href=\"", iGenre);
  471.     if (iStart < 0)
  472.       iStart = strBody.IndexOf("HREF=\"", iGenre);
  473.     if (iStart < 0)
  474.       return "";
  475.       
  476.     int iEnd = strBody.IndexOf("</a>", iStart);
  477.     if (iEnd < 0)
  478.       iEnd = strBody.IndexOf("</A>", iStart);
  479.     if (iEnd < 0)
  480.       return "";
  481.     
  482.     iStart = strBody.IndexOf(">", iStart);
  483.     iStart++;
  484.     string strTitle = strBody.Substring(iStart, iEnd - iStart);
  485.     strTitle = MediaPortal.Util.Utils.stripHTMLtags(strTitle);
  486.     HTMLUtil htmlUtil = new HTMLUtil();
  487.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  488.     strTitle = strTitle.Trim();
  489.     return strTitle.Trim();
  490.   }
  491.  
  492.  
  493. }